package com.peas.platform.module.icims.budget.apply.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.common.core.dto.ResponseDto;
import com.peas.platform.framework.mybatisplus.utils.PageUtil;
import com.peas.platform.module.icims.budget.apply.api.req.AdjustBudgetTwoAmount;
import com.peas.platform.module.icims.budget.apply.api.req.SaveBudgetTwoCollectVo;
import com.peas.platform.module.icims.budget.apply.api.req.SaveBudgetTwoVo;
import com.peas.platform.module.icims.budget.apply.api.req.SubmitBudgetTwoVo;
import com.peas.platform.module.icims.budget.apply.controller.criteria.BudgetTwoCollectCriteria;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoCollectDto;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoDto;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoCollectService;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "自定义接口-[预算控制]二上预算编制")
@RestController
@RequestMapping("/api/v1/customBudgetTwo")
public class CustomBudgetTwoApiController {

    @Resource
    private BudgetTwoService budgetTwoService;
    @Resource
    private BudgetTwoCollectService budgetTwoCollectService;

    @ApiOperation(value = "编辑暂存二上预算")
    @PostMapping("/stageBudgetTwo")
    public ResponseDto<BudgetTwoDto> stageBudgetTwo(@RequestBody SaveBudgetTwoVo vo) {
        BudgetTwoDto dto = budgetTwoService.saveBudgetTwo(vo);
        return ResponseDto.getSuccessResponseDto(dto);
    }

    @ApiOperation(value = "编辑保存二上预算")
    @PostMapping("/saveBudgetTwo")
    public ResponseDto<Boolean> saveBudgetTwo(@RequestBody @Valid SaveBudgetTwoVo vo) {
        budgetTwoService.saveBudgetTwo(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "编辑提交二上预算")
    @PostMapping("/submitBudgetTwo")
    public ResponseDto<Boolean> submitBudgetTwo(@RequestBody @Valid SubmitBudgetTwoVo vo) {
        budgetTwoService.submitBudgetTwo(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "撤回二上预算")
    @PostMapping("/revocationBudgetTwo/{id}")
    public ResponseDto<Boolean> revocationBudgetTwo(@PathVariable("id") Long id) {
        budgetTwoService.revocationBudgetTwo(id);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "根据ID查询二上预算，包含统计金额")
    @GetMapping("/queryBudgetTwoByIds/{ids}")
    public ResponseDto<List<BudgetTwoDto>> queryBudgetTwoByIds(@PathVariable(value = "ids") String ids) {
        List<Long> idList = new ArrayList<>();
        for (String id : StrUtil.split(ids, StrUtil.COMMA)) {
            idList.add(Long.valueOf(id));
        }
        List<BudgetTwoDto> list = budgetTwoService.queryBudgetTwoById(idList);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据一级项目ID查询二上预算，包含统计金额")
    @GetMapping("/queryBudgetTwoByProjectOneId/{id}")
    public ResponseDto<List<BudgetTwoDto>> queryBudgetTwoByProjectOneId(@PathVariable(value = "id") Long id) {
        List<BudgetTwoDto> list = budgetTwoService.queryBudgetTwoByProjectOneId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据组织架构ID查询二上预算，包含统计金额")
    @GetMapping("/queryBudgetTwoByOrganizationId/{id}")
    public ResponseDto<List<BudgetTwoDto>> queryBudgetTwoByOrganizationId(@PathVariable(value = "id") Long id) {
        List<BudgetTwoDto> list = budgetTwoService.queryBudgetTwoByOrganizationId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据申报单位ID查询二上预算，包含统计金额")
    @GetMapping("/queryBudgetTwoByApplyGovernmentId/{id}")
    public ResponseDto<List<BudgetTwoDto>> queryBudgetTwoByApplyGovernmentId(@PathVariable(value = "id") Long id) {
        List<BudgetTwoDto> list = budgetTwoService.queryBudgetTwoByApplyGovernmentId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "根据用款单位ID查询二上预算，包含统计金额")
    @GetMapping("/queryBudgetTwoByUseGovernmentId/{id}")
    public ResponseDto<List<BudgetTwoDto>> queryBudgetTwoByUseGovernmentId(@PathVariable(value = "id") Long id) {
        List<BudgetTwoDto> list = budgetTwoService.queryBudgetTwoByUseGovernmentId(id);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "编辑保存二上预算汇总")
    @PostMapping("/saveBudgetTwoCollect")
    public ResponseDto<Boolean> saveBudgetTwoCollect(@RequestBody @Valid SaveBudgetTwoCollectVo vo) {
        budgetTwoCollectService.saveBudgetTwoCollect(vo);
        return ResponseDto.getSuccessResponseDto(true);
    }

    @ApiOperation(value = "查询二上预算汇总，包含统计金额")
    @GetMapping("/queryBudgetTwoCollectTotalAmount")
    public ResponseDto<Page<BudgetTwoCollectDto>> queryBudgetTwoCollectTotalAmount(BudgetTwoCollectCriteria condition,
                                                                                   @ApiParam(name = "pageNum", value = "分页页码", example = "1") Integer pageNum,
                                                                                   @ApiParam(name = "pageSize", value = "分页每页大小", example = "10") Integer pageSize,
                                                                                   @ApiParam(name = "sortField", value = "排序字段", example = "id") String sortField,
                                                                                   @ApiParam(name = "sortType", value = "排序方式", example = "desc") String sortType) {
        Page<BudgetTwoCollectDto> pager = PageUtil.genPager(pageNum, pageSize, sortField, sortType);
        BudgetTwoCollectDto c = BeanUtil.copyProperties(condition, BudgetTwoCollectDto.class);
        Page<BudgetTwoCollectDto> dtos = budgetTwoCollectService.page(pager, c);
        if (dtos != null && CollUtil.isNotEmpty(dtos.getRecords())) {
            for (BudgetTwoCollectDto dto : dtos.getRecords()) {
                BigDecimal totalAmount = budgetTwoCollectService.calcBudgetTwoCollectTotalAmount(dto);
                dto.setTotalAmount(totalAmount);
            }
        }
        return ResponseDto.getSuccessResponseDto(dtos);
    }

    @ApiOperation(value = "调整二下审批金额")
    @PostMapping("/adjustBudgetTwoAmount")
    public ResponseDto<Boolean> adjustBudgetTwoAmount(@RequestBody @Valid AdjustBudgetTwoAmount vo) {
        budgetTwoService.adjustBudgetTwoAmount(vo.getItems());
        return ResponseDto.getSuccessResponseDto(true);
    }

}
